function replaceForm() {
    const loginTmp = `
                                          <input type="hidden" name="action_type" value="login" id="action_type">
                                   <label for="account">账号</label>
                                   <input type="text" class="form-control" name="account" id="account" placeholder="输入账号...">
                                   <label for="password">密码</label>
                                   <input type="password" class="form-control" name="password" id="password" placeholder="输入密码...">
                                    <span style="font-size: small">没有帐号?<a href="#" onclick="replaceForm()">点击注册</a></span>
                                     <span style="font-size: small;color: red;float: right" id="form-fail-msg"></span>
                                         `;
    const regTmp = `
                                          <input type="hidden" name="action_type" value="register" id="action_type">
                                   <label for="account">账号</label>
                                   <input type="text" class="form-control" name="account" id="account" placeholder="输入账号...">
                                   <label for="password">密码</label>
                                   <input type="password" class="form-control" name="password" id="password" placeholder="输入密码...">
                                    <label for="re_password">确认密码</label>
                                    <input type="password" class="form-control" name="re_password" id="re_password" placeholder="确认密码...">
                                    <span style="font-size: small">已有帐号?<a href="#" onclick="replaceForm()">点击登录</a></span>
                                     <span style="font-size: small;color: red;float: right" id="form-fail-msg"></span>
                                         `;
    let type = $('#action_type').val();
    if (type === 'login') {
        $('#form-replace-able-title').text('注册');
        $('#form-replace-able-content').html(regTmp);
        $('#form-replace-able-btn').text('注册');
        $('#form-replace-able-form').attr('action', '/user/register');
    } else {
        $('#form-replace-able-title').text('登录');
        $('#form-replace-able-content').html(loginTmp);
        $('#form-replace-able-btn').text('登录');
        $('#form-replace-able-form').attr('action', '/user/login');
    }
}

function submitForm() {
    let action = $('#action_type').val();

    const url = $('#form-replace-able-form').attr('action');
    let account = $('#account').val();
    let password = $('#password').val();
    let repsw = $('#re_password').val();

    //验证
    if (account == '') {
        $('#form-fail-msg').text("用户名不能为空");
        return;
    }
    if (password == '') {
        $('#form-fail-msg').text("密码不能为空");
        return;
    }
    if (action === 'register') {
        if (password != repsw) {
            $('#form-fail-msg').text("两次密码不一致");
            return;
        }
    }
    //发送登录/注册请求
    $.ajax({
        url: url,
        method: 'post',
        data: {
            account: account,
            password: password
        },
        success(res) {
            if (res.result == true) {
                //登录成功,在localstorage中存储token
                localStorage.setItem('jwt-token', "Bearer " + res.data.token);
                //刷新页面
                window.location.reload();
            } else {
                $('#form-fail-msg').text(res.message);
            }
        }
    });
}

//退出登录
function quitLogin() {
    $.ajax({
        url: "/user/offline",
        method: "POST",
        success(res) {
            if (res.result == true) {
                //清除本地jwt-token
                localStorage.removeItem('jwt-token');
                alert("退出登录成功,下线时间:" + res.data);
                window.location.reload();
            } else {
                alert("用户还未登录");
            }
        }
    })
}
